package com.study.controller;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.study.entity.common.CommonRequestEntity;
import com.study.entity.common.CommonResponseEntity;
import com.study.entity.db3.Customer;
import com.study.enumeration.ResponseEnum;
import com.study.exception.DefinitionException;
import com.study.service.db3.ICustomerService;
import com.study.util.ResponseUtil;

/**
 * 客户表 前端控制器
 * 
 * @author chenk
 *
 */
@Controller
@RequestMapping("/customer")
public class CustomerController {

	/**
	 * 日志
	 */
	private static final Logger LOGGER = LoggerFactory.getLogger(CustomerController.class);

	@Autowired
	private ICustomerService customerService;

	/**
	 * 进入列表页
	 * 
	 * @return
	 */
	@GetMapping("/toList")
	public String toList() {
		return "/customer/customerList";
	}

	/**
	 * 查询列表
	 * 
	 * @param requestEntity
	 * @return
	 */
	@GetMapping("/list")
	@ResponseBody
	public CommonResponseEntity<Map<String, Object>> list(CommonRequestEntity requestEntity) {
		PageHelper.startPage(requestEntity.getPage(), requestEntity.getLimit());
		Page<Customer> customers = customerService.queryCustomers(requestEntity.getRequestParams());
		return ResponseUtil.buildPage(customers);
	}

	/**
	 * 进入新增页
	 * 
	 * @return
	 */
	@GetMapping("toAdd")
	public String toAdd() {
		return "customer/customerAdd";
	}

	/**
	 * 新增客户
	 * 
	 * @param customer
	 * @return
	 */
	@PostMapping
	@ResponseBody
	public CommonResponseEntity<Object> add(@RequestBody Customer customer) {
		try {
			customerService.saveCustomer(customer);
			return ResponseUtil.success();
		} catch (Exception e) {
			LOGGER.error("保存失败：" + e.getMessage());
			throw new DefinitionException(ResponseEnum.OPR_FAIL);
		}
	}

	/**
	 * 进入更新页面
	 * 
	 * @return
	 */
	@GetMapping("toUpdate/{id}")
	public String toUpdate(@PathVariable Long id, Model model) {
		Customer customer = customerService.getById(id);
		model.addAttribute("customer", customer);
		return "customer/customerUpdate";
	}

	/**
	 * 修改客户
	 * 
	 * @param customer
	 * @return
	 */
	@PutMapping
	@ResponseBody
	public CommonResponseEntity<Object> updateSubmit(@RequestBody Customer customer) {
		try {
			customerService.updateCustomer(customer);
			return ResponseUtil.success();
		} catch (Exception e) {
			LOGGER.error("修改失败：" + e.getMessage());
			throw new DefinitionException(ResponseEnum.OPR_FAIL);
		}
	}

	/**
	 * 进入详情页
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@GetMapping("toDetail/{id}")
	public String toDetail(@PathVariable Long id, Model model) {
		Customer customer = customerService.getById(id);
		model.addAttribute("customer", customer);
		return "customer/customerDetail";
	}

	/**
	 * 删除客户
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@DeleteMapping("/{id}")
	@ResponseBody
	public CommonResponseEntity<Object> delete(@PathVariable Long id, Model model) {
		try {
			customerService.deleteById(id);
			return ResponseUtil.success();
		} catch (Exception e) {
			LOGGER.error("删除失败：" + e.getMessage());
			throw new DefinitionException(ResponseEnum.OPR_FAIL);
		}
	}

}
